package com.example.casper.Experiment2024.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.data.DataLoader;
import com.example.casper.Experiment2024.data.ShopLocation;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
import com.tencent.tencentmap.mapsdk.maps.model.Marker;
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;

import java.util.ArrayList;

public class TencentMapsFragment extends Fragment {
    private com.tencent.tencentmap.mapsdk.maps.TextureMapView mapView = null; // 声明地图视图

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 初始化地图视图
        View rootView = inflater.inflate(R.layout.fragment_tencent_map, container, false);
        mapView = rootView.findViewById(R.id.mapView);

        // 获取腾讯地图对象
        TencentMap tencentMap = mapView.getMap();

        // 设置初始地图位置为暨大珠区
        LatLng point1 = new LatLng(22.249942, 113.534341);
        tencentMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point1, 16)); // 设置地图缩放级别为16
        MarkerOptions markerOptions = new MarkerOptions(point1).title("暨大珠区");
        tencentMap.addMarker(markerOptions); // 添加标记

        // 设置标记点击事件监听器
        tencentMap.setOnMarkerClickListener(marker -> {
            Toast.makeText(getContext(), marker.getTitle(), Toast.LENGTH_SHORT).show(); // 显示点击标记的标题
            return true;
        });

        // 新线程加载并显示商店数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 下载商店数据
                String responseData = new DataLoader().download("https://file.notion.so/f/f/3500cdb4-2af2-4803-b797-b49933980410/8332c677-d2bd-461f-97da-5a84839038f4/shop.json?table=block&id=143148a1-26c1-8090-acdf-d425c3687691&spaceId=3500cdb4-2af2-4803-b797-b49933980410&expirationTimestamp=1735113600000&signature=4jrJ8yx9mAqcECredAg9uxXXsgbsSEpBMeXohc9AlQ0&downloadName=shop.json");

                // 解析商店数据为ShopLocation对象列表
                ArrayList<ShopLocation> shopLocations = new DataLoader().parseJsonObjects(responseData);

                // 在主线程中更新UI，显示商店的位置
                requireActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // 获取腾讯地图对象
                        TencentMap tencentMap = mapView.getMap();

                        // 遍历商店数据并在地图上添加标记
                        for (ShopLocation shopLocation : shopLocations) {
                            LatLng shopPoint = new LatLng(shopLocation.getLatitude(), shopLocation.getLongitude());
                            MarkerOptions shopMarkerOptions = new MarkerOptions(shopPoint)
                                    .title(shopLocation.getName()); // 设置标记的标题为商店名称
                            Marker shopMarker = tencentMap.addMarker(shopMarkerOptions); // 添加标记到地图
                        }
                    }
                });
            }
        }).start(); // 启动线程下载和处理商店数据

        return rootView; // 返回视图
    }
}
